package com.Zhu.demo.controller;

import com.Zhu.demo.pojo.User;

import com.Zhu.demo.responseMessage.ResponseMessage;
import com.Zhu.demo.service.UserService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

@RestController
@RequestMapping("/user")
@Api(tags = "用户管理接口")
public class UserController {

    @Autowired
    UserService userService;
    @GetMapping("/userAll")
    @ApiOperation("查询所有用户")
    public ResponseMessage<List<User>> User(){
        Iterable<User> user = userService.SelectUserAll();
        List<User> userList =  StreamSupport.stream(user.spliterator(), false)
                .collect(Collectors.toList());
        return ResponseMessage.success(userList);
    }

    @GetMapping("/selectUser/{userId}")
    @ApiOperation("查询单个用户")
    public ResponseMessage<User> User(@PathVariable Integer userId){
        User user = userService.SelectUser(userId);
        return ResponseMessage.success(user);
    }

    @PostMapping("/addUser")
    @ApiOperation("添加用户")
    public ResponseMessage<User> UserAdd(@Valid @RequestBody User user){
        userService.UserAdd(user);
        return ResponseMessage.success(user);
    }

    @DeleteMapping("/deleteUser/{userId}")
    @ApiOperation("删除用户")
    public ResponseMessage<User> UserDelete(@PathVariable Integer userId){
        userService.UserDelete(userId);
        return ResponseMessage.success(null);
    }

    @PutMapping("/updateUser/{userId}")
    @ApiOperation("修改用户")
    public ResponseMessage<User> UserUpdate(@PathVariable Integer userId,@Valid @RequestBody User user){
        user.setUserId(userId);
        userService.UserUpdate(user);
        return ResponseMessage.success(user);
    }

}
